from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeSolid, BRepBuilderAPI_MakeFace, BRepBuilderAPI_MakeEdge, \
    BRepBuilderAPI_MakeWire
from OCC.Core.BRepGProp import brepgprop_VolumeProperties
from OCC.Core.GProp import GProp_GProps
from OCC.Core.TopAbs import TopAbs_SHELL
from OCC.Core.TopExp import TopExp_Explorer
from OCC.Core.TopoDS import topods
from OCC.Display.SimpleGui import init_display
from OCC.Extend.DataExchange import read_stl_file
from OCC.Core.gp import gp_Pnt
from OCC.Core.GC import GC_MakeSegment


def calc_volume(shape):
    props = GProp_GProps()
    brepgprop_VolumeProperties(shape, props)
    return props.Mass()


width = 1000
length = 3500

p0 = gp_Pnt(width / 2, length / 2, 0)
p1 = gp_Pnt(width / 2, -length / 2, 0)
p2 = gp_Pnt(-width / 2, -length / 2, 0)
p3 = gp_Pnt(-width / 2, length / 2, 0)
s0 = GC_MakeSegment(p0, p1)
s1 = GC_MakeSegment(p1, p2)
s2 = GC_MakeSegment(p2, p3)
s3 = GC_MakeSegment(p3, p0)
e0 = BRepBuilderAPI_MakeEdge(s0.Value())
e1 = BRepBuilderAPI_MakeEdge(s1.Value())
e2 = BRepBuilderAPI_MakeEdge(s2.Value())
e3 = BRepBuilderAPI_MakeEdge(s3.Value())

rect = BRepBuilderAPI_MakeWire(e0.Edge(), e1.Edge(), e2.Edge(), e3.Edge())
face = BRepBuilderAPI_MakeFace(rect.Wire())

shapes = read_stl_file('./suidao.STL')

explorer = TopExp_Explorer(shapes, TopAbs_SHELL)

boxes = []
while explorer.More():
    shell = topods.Shell(explorer.Current())
    print(shell)
    ms = BRepBuilderAPI_MakeSolid()
    ms.Add(shell)
    sld = ms.Solid()
    boxes.append(sld)
    print(sld, calc_volume(sld))
    explorer.Next()

print(boxes)

display, start_display, add_menu, add_function_to_menu = init_display()

display.DisplayShape(boxes)
display.DisplayShape(face.Shape())

start_display()
